﻿#include "BombSpider.h"
#include "../../MirEnvir/Envir.h"
#include "../DelayedAction.h"
#include "../MapObject.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Enums.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    BombSpider::BombSpider(MonsterInfo *info) : MonsterObject(info)
    {
        ExplosionTime = getEnvir()->getTime() + 1000 * 60 * 5;
    }

    void BombSpider::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            Die();
            return;
        }
        if (InAttackRange())
        {
            Die();
            return;
        }
        if (getEnvir()->getTime() > ExplosionTime)
        {
            Die();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }

    void BombSpider::Die()
    {
        DelayedAction tempVar(DelayedType::Die, getEnvir()->getTime() + 500);
        ActionList.push_back(&tempVar);
        MonsterObject::Die();
    }

    void BombSpider::CompleteDeath(std::vector<std::any> &data)
    {
        std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation(), false);
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
                return;
            }

            if (targets[i]->Attacked(this, damage, DefenceType::ACAgility) <= 0)
            {
                continue;
            }

            if (getEnvir()->Random->Next(Settings::PoisonResistWeight) >= targets[i]->Stats[Stat::PoisonResist])
            {
                if (getEnvir()->Random->Next(5) == 0)
                {
                    Poison *tempVar = new Poison(), *this;
                    tempVar->Owner = this;
                    tempVar->Duration = 5;
                    tempVar->PType = PoisonType::Green;
                    tempVar->Value = GetAttackPower(Stats[Stat::MinSC], Stats[Stat::MaxSC]);
                    tempVar->TickSpeed = 2000;
                    targets[i]->ApplyPoison(tempVar, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                }
            }
        }
    }
}
